﻿Imports System.Data
Imports System.Data.Odbc
Imports System.Text
Imports System.Collections.Generic
Imports WSEntidades
Imports WSFactory

Public Class BancosDAL
    Public Function registraBancos(ByVal pBanco As BancosBE) As Integer
        Dim instrSql As String
        Dim oInsParm(0 To 3) As OdbcParameter
        Try

            instrSql = "INSERT INTO ""Bancos""(""IdBanco"", ""Descripcion"", ""Status"" " & _
                                        "VALUES (?,?,?);"

            oInsParm(0) = New OdbcParameter("", pBanco.IdBanco)
            oInsParm(1) = New OdbcParameter("", pBanco.Descripcion)
            oInsParm(2) = New OdbcParameter("", pBanco.Status)


            'Crea el objeto connection
            Using myConn As OdbcConnection = New OdbcConnection(My.Settings.cs)
                Using myComm As New OdbcCommand(instrSql, myConn)
                    myComm.CommandType = CommandType.Text
                    myComm.Parameters.AddRange(oInsParm)
                    myConn.Open()
                    myComm.ExecuteNonQuery()
                    If (myComm.ExecuteNonQuery() > 0) Then
                        Return True
                    Else
                        Return False
                    End If
                End Using
            End Using

        Catch ex As Exception
            Throw
        Finally
            oInsParm = Nothing
            instrSql = Nothing
        End Try
    End Function

    Public Function modificaBanco(ByVal pBanco As BancosBE) As Boolean
        Dim instrsql As String
        Dim oUpdParm(0 To 2) As OdbcParameter
        Try
            instrsql = "UPDATE Bancos " & _
                            "SET ""Descripcion"" = ?, " & _
                                                """status"" = ? " & _
                                                "WHERE ""IdBanco"" = ?; "

            oUpdParm(0) = New OdbcParameter("", pBanco.Descripcion)
            oUpdParm(1) = New OdbcParameter("", pBanco.Status)
            oUpdParm(2) = New OdbcParameter("", pBanco.IdBanco)
            'Crea el objeto connection
            Using myConn As OdbcConnection = New OdbcConnection(My.Settings.cs)
                Using myComm As New OdbcCommand(instrsql, myConn)
                    myComm.CommandType = CommandType.Text
                    myComm.Parameters.AddRange(oUpdParm)
                    myConn.Open()
                    myComm.ExecuteNonQuery()
                    If (myComm.ExecuteNonQuery() > 0) Then
                        Return True
                    Else
                        Return False
                    End If
                End Using
            End Using
            
        Catch ex As Exception
            Throw
        Finally
            oUpdParm = Nothing
            instrsql = Nothing
        End Try
    End Function

    Public Function getBancoById(ByVal pId As Integer) As BancosBE
        Dim instrSql As String
        Dim tmpBan As BancosBE
        Try
            instrSql = "SELECT ""Descripcion"", ""IdBanco"", ""Status"" " & _
                                     "FROM ""Bancos"" " & _
                                    "WHERE ""IdBanco"" = ? ;"

            Using myConn As OdbcConnection = New OdbcConnection(My.Settings.cs)
                Using myComm As New OdbcCommand(instrSql, myConn)
                    myComm.CommandType = CommandType.Text
                    myComm.Parameters.Add(New OdbcParameter("", pId))
                    myConn.Open()
                    Using myDR As OdbcDataReader = myComm.ExecuteReader()
                        If (myDR.HasRows()) Then
                            myDR.Read()
                            tmpBan = bancosFact.Build(myDR)
                        Else
                            tmpBan = Nothing
                        End If
                    End Using
                End Using
            End Using
            Return tmpBan
            
        Catch ex As Exception
            Throw 
        Finally
            tmpBan = Nothing
            instrSql = ""
        End Try
    End Function

    Public Function existeBanco(ByVal pBanco As String) As Boolean
        Dim existe As Boolean = False
        Dim instrSql As String
        Dim oSelParm(0 To 0) As OdbcParameter
        Try

            instrSql = "SELECT COUNT(*) " & _
                                    "FROM ""Bancos"" " & _
                                    "WHERE ""IdBanco"" = ? ;"

            oSelParm(0) = New OdbcParameter("", pBanco)
            Dim count As Object = db.ExecScalar(instrSql, CommandType.Text, oSelParm, My.Settings.cs)
            If (Not count Is Nothing) Then
                If (CShort(count) > 0) Then
                    existe = True
                Else
                    existe = False
                End If
            Else
                existe = False
            End If
            Return existe
            

        Catch ex As Exception
            Throw 
        Finally
            existe = Nothing
            oSelParm = Nothing
            instrSql = Nothing
        End Try
    End Function
End Class
